<?php
/**
 * @filesource modules/comment/controllers/IndexController.php
 * @author Dao Quang Duc
 * @abstract : List of actions in order
 *   - new
 *   - view
 *   - edit
 *   - delete
 *   - search
 */ 
class Comment_IndexController extends Clu_Controller_Action_Hvn
{
	public $m, $mPlace, $mArticle;
	
	public function init()
	{
		//require_once (APP_PATH.'/modules/place/Model/Place.php');
		//require_once (APP_PATH.'/modules/event/Model/Event.php');
		//require_once (APP_PATH.'/modules/article/Model/Article.php');
		$this->mPlace = new place_Model_Place();
		$this->mArticle = new article_Model_Service();
		$this->mEvent = new event_Model_Service();
		$this->m = new comment_Model_Service();
		
		parent::init();
	}
	
	public function editAction() 
	{
		$commentId = $this->_request->getParam("id");
		if (!$c = $this->m->findByPrimary($commentId))
        {
            $this->view->errorMessage = __(603);
            $this->renderToHolder('common/error.phtml');
            return;
        }	
        
        //everything's fine. Continue editing
        $form = new comment_Form_Comment();
        $form->cluBuild(array('action' => 'update','control' => 'index'));
        
        //populate the data with 
		if (!$this->_request->isPost())
            $form->populate($c->toArray());
        else
        { 
//        	die_a($this->_request->getParams());
            if ($form->isValid($_POST))
            {	
            	
                if ($this->m->saveRaw($c, $this->_request->getParams(), $commentId, false))
                {
                    $this->view->operation = __("update comment");
                    $this->renderToHolder('common/success.phtml');
                    return;
                }
                else 
                    $this->view->errorMessage = __(607);
            }
            else 
            	$this->view->errorMessage = __(71);
          //  die();
        }
        $this->view->form = $form;
        $this->view->id = $commentId;
        $this->renderToHolder('index/edit.phtml');    
	}
	
	public function viewAction() 
	{
		$data = $this->_request->getParams();
		
		//Search comment form
		$formSearch = new comment_comment_Form_CommentSearch();
        $formSearch->cluBuild(array('control' => 'service'));
		$this->view->formSearch = $formSearch;

		//new Comment Box
		$form2 = new comment_Form_Comment();
		$form2->cluBuild(array('action' => 'new','control' => 'ajax'));
		$this->view->form2 = $form2;
		
		//comment list + pagination
       	$select = $this->m->search($data);
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
		$paginator->setItemCountPerPage($data['comments_per_page']);
		$paginator->setCurrentPageNumber($data['page_number']);
		Zend_Paginator::setDefaultScrollingStyle('Jumping');
		$this->view->paginator = $paginator;
		$this->view->cList = $paginator->getCurrentItems();
		//TODO: Set page number to HTML
		$this->view->commentList = $this->view->render('en/index/commentList.phtml');
		
		//additional info: parent type, parent id
		$this->view->content_type = $data['content_type'];
		$this->view->content_id = $data['content_id'];
		//Everything put together
       	$this->renderToHolder('index/view.phtml');
	}
	
	public function deleteAction() 
	{
		if (!$id = $this->_request->getParam('id'))
    	{
    		$this->view->errorMessage = __(608);
    		$this->renderToHolder('common/error.phtml');
            return;
        }
        if ($this->m->deleteByPrimary($id))
        {
            $this->view->operation = __(604);
            $this->renderToHolder( 'common/success.phtml');
        }
		else
		{   
			$this->view->errorMessage = __(605);
            $this->renderToHolder('common/error.phtml');
		} 
	}
	
	public function searchAction()
    {
    	$data = $this->_request->getParams();
      	$form = new comment_comment_Form_CommentSearch();
      	$form->cluBuild(array('control' => 'index'));
        $this->view->form = $form; 
		if($this->_request->isPost() && $form->isValid($_POST))
		{
	    	if($paginator = $this->m->search($data))
    		{
    			$paginator = $this->generatePaginator($paginator, 1, $data['comments_per_page'], 3, 'Jumping');
                $form->populate($data);  
    			
    			//cache in $_SESSION
               	$sess = new Zend_Session_Namespace("comment_paginator");
                $sess->comment_admin = array("search_params" => $data); 
        	}
		}
        else if($data['page'] != null){
			$sess = new Zend_Session_Namespace("comment_paginator");
        	$data = $sess->comment_admin;
           	if(isset($data))
           	{
               $select = $this->m->search($data['search_params']);
               $paginator = $this->generatePaginator($select, $this->_getParam('page'), $data['search_params']['comments_per_page'], 3, 'Jumping');
               //populate form
               $form->populate($data['search_params']);
           	}
        }

        if(isset($paginator))
       	{                
            $this->view->paginator = $paginator;
    		$this->view->commentList = $paginator->getCurrentItems();//list of found comments
    		$formComment = new comment_Form_Comment();
        	$formComment->cluBuild(array('action' => 'new','control' => 'ajax'));
        	$this->view->formComment = $formComment;
            $this->renderToHolder('index/list.phtml',false, null, 4,2); 
       	}
        	$this->renderToHolder('index/search.phtml',false, null, 4,1); //search box 
    }
    
	public function generatePaginator($select, $pageNumber, $itemsPerPage, $pageRange, $style)
    {
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
        $paginator->setItemCountPerPage($itemsPerPage);
        $paginator->setCurrentPageNumber($pageNumber);
        $paginator->setPageRange($pageRange);
        Zend_Paginator::setDefaultScrollingStyle($style);
        return $paginator;
    }
    
	public function successAction()
    {
         $this->view->commentId =  $this->_request->getParam("lastInsertId");
         $this->renderToHolder( 'common/success.phtml');  
    }
    
}
?>
